package com.superpai.activity;

import java.util.List;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.superpai.R;
import com.superpai.api.ShopAPI;
import com.superpai.cache.CacheCategory;
import com.superpai.common.Constants;
import com.superpai.domain.ShopCategory;
import com.superpai.response.shop.GetShopCategoryListResponse;
import com.superpai.view.adapter.CategoryAdapter;

/**
 * @author Devin.Hu
 * @date 2011-8-13
 * @version V1.0
 * @description 分类页面
 */
public class CategoryActivity extends BaseActivity implements OnItemClickListener{
	
	private final int GETCATEGORY = 1;
	private GetShopCategoryListResponse result;
	private List<ShopCategory> mList;
	private CategoryAdapter mAdapter;
	private ListView category_list;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category);
        initMenuView(this, mIndexBrand);
        initViews();
        
    }
    
    /** 初始化页面  **/
    private void initViews(){
    	title = (TextView)this.findViewById(R.id.title);
		title.setText(R.string.menu_brand);
		mProgress = (ProgressBar)this.findViewById(R.id.progress);
		mRefresh = (Button)this.findViewById(R.id.refresh);
		mRefresh.setOnClickListener(this);
		mNodata = (TextView)this.findViewById(R.id.nodata);
		
    	mAdapter = new CategoryAdapter(mContext);
    	category_list= (ListView)this.findViewById(R.id.category_list);
    	category_list.setAdapter(mAdapter);
    	category_list.setOnItemClickListener(this);
    	
    	connectionDate();
    }
    
    
    /** 读取店铺分类数据 **/
    private void connectionDate() {
		mRefresh.setVisibility(View.GONE);
		mNodata.setVisibility(View.GONE);
		mProgress.setVisibility(View.VISIBLE);
		onConnection(GETCATEGORY);
	}
	
	@Override
	public Object doDataConnection(int type) {
		try {
			CacheCategory cacheCategory = new CacheCategory(mContext);
			if(cacheCategory.checkCacheCateInfo()){
				mList = cacheCategory.getCacheCateInfo();
				return STATUS_SUC;
			}else{
				ShopAPI api = new ShopAPI();
				result = api.getShopCategoryList(Constants.sellerUin);
				if(result.isSuccess()){
					mList = result.getShopCategoryList();
					cacheCategory.addCacheCateInfo(mList);
					return STATUS_SUC;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return STATUS_ERR;
		}
		return STATUS_NODATA;
	}

	@Override
	public void doProcessData(int type, Object... values) {
		disConnection();
		int status = Integer.parseInt(values[0].toString());
		mProgress.setVisibility(View.GONE);
		
		if(status == STATUS_SUC){
			mAdapter.setmList(mList);
			mAdapter.notifyDataSetChanged(); 
		}
		else if(status == STATUS_NODATA){
			mNodata.setVisibility(View.VISIBLE);
		}
		else{
			mRefresh.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	public void onClick(View v) {
		checkMenu(v);
		
		if(v == mRefresh){
			connectionDate();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		String categoryId = mList.get(position).getCategoryId();
		String categoryName = mList.get(position).getDesc();
		Intent intent = new Intent();
		intent.putExtra("categoryId", categoryId);
		intent.putExtra("categoryName", categoryName);
		intent.putExtra("fromtype", "home");
		intent.setClass(mContext, ProductsActivity.class);
		mContext.startActivity(intent);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			new AlertDialog.Builder(this)
			.setTitle(R.string.common_tishi)
			.setMessage(R.string.common_sys_exit)
			.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					sysExit();
				}
			})
			.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.show();
			return true;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		disConnection();
	}
	
}